﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Public;
using BUS;
namespace QuanLyKhoHangVatTu
{
    public partial class NhomKhachHangNCC : DevExpress.XtraEditors.XtraForm
    {
        NhomKHNCC_PB nhom_pb = new NhomKHNCC_PB();
        NhomKHNCC_BUS nhom_bus = new NhomKHNCC_BUS();
        public NhomKhachHangNCC()
        {
            InitializeComponent();
        }
        public NhomKhachHangNCC(string ma, string ten, string diengiai, string loai)
        {
            InitializeComponent();

            txtMa.Text = ma;
            txtTen.Text = ten;
            txtDienGiai.Text = diengiai;
            cboLoai.Text = loai;
            txtMa.Properties.ReadOnly = true;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
        }

        private void NhomKhachHangNCC_Load(object sender, EventArgs e)
        {
            cboLoai.SelectedItem = "Khách Hàng";
        }

        public NhomKHNCC_PB GetValues()
        {
            NhomKHNCC_PB.MaNhom = txtMa.Text;
            NhomKHNCC_PB.TenNhom = txtTen.Text;
            NhomKHNCC_PB.DienGiai = txtDienGiai.Text;
            NhomKHNCC_PB.Loai = cboLoai.SelectedItem.ToString();
            return nhom_pb;
        }
        private bool CheckData()
        {
            if (txtMa.Text == "" || txtTen.Text == "" )
                return false;
            if (txtMa.Text == string.Empty)
            {
                txtMa.ErrorText = "Mã nhóm không được trống";
                return false;
            }
            if (txtTen.Text == string.Empty)
            {
                txtTen.ErrorText = "Tên nhóm không được trống";
                return false;
            }
            if (nhom_bus.Exists(txtMa.Text))
            {
                if (Flag_PB.Sua == true)
                {
                    Flag_PB.Sua = false;
                    return true;

                }
                txtMa.ErrorText = "Mã trùng";
                return false;
            }
            return true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            GetValues();
            if (CheckData())
            {   
                try
                {
                    nhom_bus.ThemSuaNhomKHNCC(nhom_pb);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Them_Click(object sender, EventArgs e)
        {
            txtMa.Properties.ReadOnly = false;
            GetValues();
            if (CheckData())
            {
                try
                {
                    nhom_bus.ThemSuaNhomKHNCC(nhom_pb);

                    txtMa.ResetText();
                    txtDienGiai.ResetText();
                    txtTen.ResetText();
                    cboLoai.ResetText();
                    if (this.Tag != null)
                    {
                        IChildForm iChildForm = this.Tag as IChildForm;
                        if (iChildForm != null)
                            iChildForm.Nap();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
            }
            
        }
    }
}